package cn.edu.sasu.service.impl;

import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.common.constants.Constant;
import cn.edu.sasu.common.util.DateUtils;
import cn.edu.sasu.domain.entity.SysMenu;
import cn.edu.sasu.domain.vo.MenuListVO;
import cn.edu.sasu.mapper.SysMenuMapper;
import cn.edu.sasu.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author 王尧
 * @since 2024-04-28
 */
@Service
public class SysMenuServiceImpl  implements ISysMenuService {
    
    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public LayuiRespResult findSysMenuAll(Integer page, Integer limit) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysMenu> sysMenus = menuMapper.findSysMenuList(Constant.DELETE_FLAG_YES, offset, rows);
        Integer totalSize = menuMapper.countSysMenu(Constant.DELETE_FLAG_YES);
        return LayuiRespResult.success(totalSize, sysMenus);
    }


    @Override
    public LayuiRespResult search(Integer page, Integer limit, String menuName) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysMenu> sysMenus = menuMapper.searchSysMenuList(Constant.DELETE_FLAG_YES, offset, rows, menuName);
        Integer totalSize = menuMapper.countSearchSysMenu(Constant.DELETE_FLAG_YES, menuName);
        return LayuiRespResult.success(totalSize, sysMenus);
    }

    @Override
    public RespResult save(SysMenu menu) {
        SysMenu sysMenu= menuMapper.findSysMenuByMenuName(menu.getMenuName());
        if(sysMenu != null){
            return RespResult.error("该菜单已经存在，请重新输入！");
        }
        menu.setCreateDate(DateUtils.getCurrentDate());
        menuMapper.save(menu);
        return RespResult.success("添加成功");
    }

    @Override
    public LayuiRespResult findSysMenus() {
        List<MenuListVO> menuList=menuMapper.getAllMenuNames();
        int size = menuList.size();
        return LayuiRespResult.success(size,menuList);
    }


    @Override
    public RespResult delSysMenuByMenuId(Integer menuId) {
        menuMapper.updateSysMenuDeleteFlagByMenuId(menuId,Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    @Override
    public RespResult batchdel(Integer[] menuIds) {
        menuMapper.updateSysMenuDeleteFlagByMenuIds(Arrays.asList(menuIds),Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    @Override
    public RespResult updateInfo(SysMenu menu) {
        menu.setUpdateDate(DateUtils.getCurrentDate());
        Integer integer = menuMapper.updateInfo(menu);
        if (integer > 0) {
            return RespResult.success("修改成功", menuMapper.findSysUserByUserId(menu.getMenuId()));
        } else {
            return RespResult.error("修改失败");
        }
    }
}
